class Solution {
    public int maximalSquare(char[][] matrix) {
        int max=0;
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
                if(matrix[i][j]=='0'){
                    continue;
                }
                int s=cal(matrix,i,j);
                if(s>max) {
                    max=s;
                }
            }
        }
        return max;

    }

    public int cal(char[][] matrix,int x,int y){
        int delta=1;
        int s=1;
        while(x+delta<matrix.length&&y+delta<matrix[x+delta].length){
            for(int i=y;i<=y+delta;i++){
                if(matrix[x+delta][i]=='0'){
                    return s;
                 }
            }
            for(int i=x;i<=x+delta;i++){
                if(matrix[i][y+delta]=='0') {
                    return s;
                } 
            }
            delta++;
            s=delta*delta;
        }
        return s;
    }
}